/**
 * 
 */
package de.cbf.cam.dsa.battle.ui.composites;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;

import de.cbf.cam.dsa.battle.ui.composites.definitions.RadioDefinition;

/**
 * @author Nebelritter
 *
 */
public class CompositeRadioArea extends Composite {
	
	/**
	 * @uml.property  name="buttons"
	 * @uml.associationEnd  multiplicity="(0 -1)" elementType="de.cbf.cam.dsa.battle.ui.composites.definitions.RadioDefinition"
	 */
	private List<Button> buttons = new ArrayList<Button>();
	/**
	 * @uml.property  name="groupRadio"
	 * @uml.associationEnd  multiplicity="(1 1)"
	 */
	private Group groupRadio;	
	/**
	 * @param parent
	 * @param style
	 */
	public CompositeRadioArea(Composite parent, int style,List<RadioDefinition>definitions) {
		super(parent, style);
		if(definitions==null|definitions.size()<1){
			dispose();
			return;
		}
		
		GridLayout layout = new GridLayout(1,false);
		layout.marginWidth = 0;
		layout.marginHeight= 0;
		setLayout(layout);
		
		groupRadio = new Group(this, SWT.NORMAL);
		groupRadio.setLayoutData(new GridData(GridData.FILL_BOTH));
		groupRadio.setLayout(new GridLayout(2,false));
		
		for (Iterator<RadioDefinition> iterator = definitions.iterator(); iterator.hasNext();) {
			RadioDefinition radioDefinition = (RadioDefinition) iterator.next();

			Button buttonRadio = new Button(groupRadio, SWT.RADIO);
			buttonRadio.setLayoutData(new GridData(SWT.FILL,SWT.CENTER,false,false,1,1));
			String buttonText=" ";
			if(radioDefinition.min==radioDefinition.max){
				buttonText += radioDefinition.min;
			}else{
				buttonText += radioDefinition.min+"-"+radioDefinition.max;
			}
			buttonRadio.setText(buttonText);
			buttonRadio.setData(radioDefinition);
			//add listeners to button
			
			Label labelRadio = new Label(groupRadio, SWT.RADIO);
			labelRadio.setLayoutData(new GridData(SWT.FILL,SWT.CENTER,false,false,1,1));
			labelRadio.setText(": "+radioDefinition.description);			
		}		
	}	
	
	public int getSelectedValue(){
		int result=Integer.MIN_VALUE;		
		for (Iterator<Button> iterator = buttons.iterator(); iterator.hasNext();) {
			Button button = (Button) iterator.next();
			if(button.getSelection()){
				RadioDefinition definition = (RadioDefinition)button.getData();
				result = (definition.min+definition.max)/2;
				break;
			}			
		}		
		return result;
	}
	
	public void setSelectedValue(int value){				
		for (Iterator<Button> iterator = buttons.iterator(); iterator.hasNext();) {
			Button button = (Button) iterator.next();
			RadioDefinition definition = (RadioDefinition)button.getData();
			if(definition.min<=value&&value<=definition.max){
				button.setSelection(true);
				break;
			}			
		}		
	}
	
	public void setTitle(String title){
		groupRadio.setText(title);
	}
}
